<template>
  <!-- 主体区域 -->
  <section id="app">
    <TodoHeader @handlerAdd="handlerAdd"></TodoHeader>
    <TodoMain :list="list" @del="del"></TodoMain>
    <TodoFooter :list="list" @clear="clear"></TodoFooter>
  </section>
</template>

<script>

import TodoHeader from './components/TodoHeader.vue';
import TodoMain from './components/TodoMain.vue';
import TodoFooter from './components/TodoFooter.vue';

export default {
  data () {
    return {
      list: [
        { id: 1, name: '打篮球' },
        { id: 2, name: '看电影' },
        { id: 3, name: '睡觉觉' }
      ]
    }
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  methods: {
    handlerAdd (todoName) {
      if (todoName == '') {
        return alert('写一点东西哦')
      }
      this.list.push({
        id: this.list.length + 1,
        name: todoName
      })

    },
    del (id) {
      this.list = this.list.filter(item => item.id != id)
    },
    clear () {
      this.list = []
    }
  },
  watch: {
    list: {
      deep: true,
      handler (value) {
        localStorage.setItem('name', JSON.stringify(value))
      }
    }
  }
}
</script>

<style>
</style>
